<?php
/**
 * 生成测试数据
 * User: wangyongqiang
 * Date: 2017/10/13 10:39
 */
require_once './redisClass.php';
class build_statlog{
    private $redis;
    private $logKey = 'log';
    private $activityAppidsKey = 'activityAppids';
    private $length = 100;
    private $logpath = 'D:/statlog.log';

    public function __construct()
    {
        //$conf = ['host'=>'10.128.36.123', 'port'=>'6379', 'pass'=>'crs-eqt107r5:qwerQWR@342WE', 'db'=>9];
        $conf = ['host'=>'127.0.0.1', 'port'=>'6379', 'pass'=>'foobared', 'db'=>9];
        $this->redis = new redisClass($conf);
    }

    public function run(){
        //构造日志队列
        $logFlag = $this->_runlog();
        echo $logFlag;

        //构造activityAppids哈希域
        //$hashFlag = $this->_buildActivityAppids();
        //echo $hashFlag;
    }

    /**
     * 构造日志队列
     */
    private function _runlog(){
        if(file_exists($this->logpath)){
            unlink($this->logpath);
        }

        $i = 0;
        while ($i < $this->length){
            $i++;

            $json = $this->_createLogData();
            $this->redis->rpush($this->logKey, $json);
        }

        unset($this->redis);

        return 1;
    }

    /**
     * 构造activityAppids哈希域
     */
    private function _buildActivityAppids(){
        $appid = 2350;

        return $this->redis->hset($this->activityAppidsKey, $appid, 1);
    }

    private function _createLogData(){
        $appidArr   = ['2350'];
        $openidArr  = [
            ['openid'=>'oLsmfwbDQFjAqk8MJ240Y_7cpGeY','nickname'=>'wayne', 'sex'=>'1', 'country'=>'US', 'province'=>'California', 'city'=>'Los Angeles'],
            ['openid'=>'oLsmfwS9sD-Z1cBRHoXOlqwQdLNY','nickname'=>'王永强', 'sex'=>'1', 'country'=>'中国', 'province'=>'北京', 'city'=>'北京'],
            ['openid'=>'oLsmfwXRlvyfO5hwwu0ydhiqqfo4','nickname'=>'json_____', 'sex'=>'1', 'country'=>'印度', 'province'=>'日凯邦', 'city'=>'孟买'],
        ];
        $os         = ['iOS/10.3.3','AndroidOS/7.0','iOS/9.1'];
        $browser    = ["MicroMessenger/6.5.16.1120",'Safari/9.0', 'Chrome/46.0.2490.76'];
        $referer    = [
            'http://bee.9tech.cn/play/tpl_sudoku/SkMzH08f/?openid=oLsmfwbDQFjAqk8MJ240Y_7cpGeY&wzid=1&friendid=oLsmfwXRlvyfO5hwwu0ydhiqqfo4&from=groupmessage&state=3fc47b68042078f5cd86554b807ac7ca',
            'http://bee.9tech.cn/play/tpl_sudoku/SkMzH08f/?openid=oLsmfwS9sD-Z1cBRHoXOlqwQdLNY&wzid=1&friendid=oLsmfwXRlvyfO5hwwu0ydhiqqfo4&from=groupmessage&state=3fc47b68042078f5cd86554b807ac7ca',
            'http://bee.9tech.cn/play/tpl_sudoku/SkMzH08f/?openid=oLsmfwXRlvyfO5hwwu0ydhiqqfo4&wzid=1&friendid=oLsmfwXRlvyfO5hwwu0ydhiqqfo4&from=groupmessage&state=3fc47b68042078f5cd86554b807ac7ca',
        ];

        $reqArr = [
            ['controller'=>'sdk', 'action'=> 'pageview'],
            ['controller'=>'wxshare', 'action'=> ['success_app_message','success_time_line','success_qq', 'success_weibo']],//'cancel_time_line'
            ['controller'=>'ad', 'action'=> 'resultpage']
        ];
        $ip2long    = ['2093943594', '2948581975', '1875774695'];
        $timestamp  = strval(time());

        $user = $openidArr[array_rand($openidArr)];
        $req = $reqArr[array_rand($reqArr)];
        if('pageview' == $req['action']){
            $data = [$appidArr[array_rand($appidArr)], $user['openid'], $req['controller'], $req['action'], $referer[array_rand($referer)], $os[array_rand($os)], $browser[array_rand($browser)], $ip2long[array_rand($ip2long)],$timestamp, $user['nickname'], $user['sex'], $user['country'], $user['province'], $user['city']];

            error_log("[{$req['action']}]".var_export($data, true)."\r\n", 3, $this->logpath);
            error_log("[{$req['action']}]".count($data)."\r\n", 3, $this->logpath);
        }else{
            $action = $req['action'];
            if('wxshare' == $req['controller']){
                $action = $action[array_rand($action)];
            }
            $data = [$appidArr[array_rand($appidArr)], $user['openid'], $req['controller'], $action, $referer[array_rand($referer)], $os[array_rand($os)], $browser[array_rand($browser)], $ip2long[array_rand($ip2long)],$timestamp];

            error_log("[{$action}]".var_export($data, true)."\r\n", 3, $this->logpath);
            error_log("[{$action}]".count($data)."\r\n", 3, $this->logpath);
        }

        return json_encode($data);
    }
}
$app = new build_statlog();
$app->run();